package com.oxy.util;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.Properties;

import org.apache.commons.logging.Log;

import com.oxy.common.constant.AppConstant;
import com.oxy.common.util.FileUtil;

public class Oxy {
	private volatile static Oxy inst;
	
	private ConfigManager cfgMgr;
	private LogManager logMgr;
	private SysErrManager sysErrMgr;
	
	// singleton
	private Oxy() {
		__init();
	}
	private void __init() {
		/*
		 * 注意在这里调用到的所有方法都不能有Oxy.xxx调用，否则会引起死循环
		 */
		
		// must have a fixed cfg entry
		String cfgFile = FileUtil.getSafePath(
				FileUtil.getClassPathAbsolute()+File.separator+
				System.getProperty(AppConstant.SYS.PROPERTY_KEY.APP_CFG_FILE));
		
		cfgMgr = new ConfigManager(cfgFile);
		logMgr = new LogManager(cfgMgr);
		sysErrMgr = new SysErrManager(logMgr);
		
		dumpConfig();
	}
	public static void init() {
		if(inst == null) {
			synchronized(Oxy.class) {
				inst = new Oxy();
			}
		}
	}
	public static Oxy getInstance() {
		init();
		return inst;
	}
	
	public static String getAppName() {
		return getInstance().cfgMgr.App().APP_NAME;
	}
	public static String getRootUrl() {
		return getInstance().cfgMgr.App().ROOTURL;
	}
	public static String getRootPath() {
		return getInstance().cfgMgr.App().ROOTPATH;
	}
	public static String getClassPath() {
		return getInstance().cfgMgr.App().CLASSPATH;
	}
	public static String getLocale() {
		return getInstance().cfgMgr.Struts().LOCALE;
	}
	
	// get item
	public static ConfigManager getCfgManager() {
		return getInstance().cfgMgr;
	}
	public static LogManager getLogManager() {
		return getInstance().logMgr;
	}
	public static SysErrManager getSysErrManager() {
		return getInstance().sysErrMgr;
	}
	
	private void dumpConfig() {
		Log logger = logMgr.getLogCommon();
		StringBuffer sb = new StringBuffer();
		try {
			sb.append("Dump of configurations:\r\n");

			sb.append("[system]\r\n");
			Field[] flds = ConfigManager.AppCfg.class.getFields();
			for(Field f : flds) {
				Object obj = f.get(cfgMgr.App());
				sb.append(f.getName()+" = "+obj+"\r\n");
			}
			sb.append("\r\n[system - raw]\r\n");
			Properties prop = cfgMgr.App();
			Enumeration<?> keys = prop.propertyNames();
			while(keys.hasMoreElements()) {
				String k = (String)keys.nextElement();
				sb.append(k+" = "+prop.getProperty(k)+"\r\n");
			}
			
			sb.append("\r\n[database]\r\n");
			flds = ConfigManager.DbCfg.class.getFields();
			for(Field f : flds) {
				Object obj = f.get(cfgMgr.Db());
				sb.append(f.getName()+" = "+obj+"\r\n");
			}
			sb.append("\r\n[database - raw]\r\n");
			prop = cfgMgr.Db();
			keys = prop.keys();
			while(keys.hasMoreElements()) {
				String k = (String)keys.nextElement();
				sb.append(k+" = "+prop.getProperty(k)+"\r\n");
			}
			
			sb.append("\r\n[struts]\r\n");
			flds = ConfigManager.StrutsCfg.class.getFields();
			for(Field f : flds) {
				Object obj = f.get(cfgMgr.Struts());
				sb.append(f.getName()+" = "+obj+"\r\n");
			}
			sb.append("\r\n[struts - raw]\r\n");
			prop = cfgMgr.Struts();
			keys = prop.keys();
			while(keys.hasMoreElements()) {
				String k = (String)keys.nextElement();
				sb.append(k+" = "+prop.getProperty(k)+"\r\n");
			}
			
			sb.append("\r\n[logging]\r\n");
			flds = ConfigManager.LogCfg.class.getFields();
			for(Field f : flds) {
				Object obj = f.get(cfgMgr.Log());
				sb.append(f.getName()+" = "+obj+"\r\n");
			}
			sb.append("\r\n[logging - raw]\r\n");
			prop = cfgMgr.Log();
			keys = prop.keys();
			while(keys.hasMoreElements()) {
				String k = (String)keys.nextElement();
				sb.append(k+" = "+prop.getProperty(k)+"\r\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
			sb.append("Error when dump configurations: "+e.getMessage());
		}
		logger.debug(sb.toString());
	}
}
